﻿#include "Tree.h"
#include "Queue.h"
BTNode* BuyBTNode(BTDataType val)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("BuyBTNode::malloc");
		return;
	}
	newnode->left = newnode->right = NULL;
	newnode->val = val;

	return newnode;
}

void Preorder(BTNode* A)
{
	if (A == NULL)
	{
		printf("NULL ");
		return;
	}
	
	printf("%c ", A->val);
	
	Preorder(A->left);
	Preorder(A->right);
}

void Inorder(BTNode* A)
{
	if (A == NULL)
	{
		printf("NULL ");
		return;
	}
	Inorder(A->left);
	printf("%c ", A->val);
	Inorder(A->right);
}


void Postorder(BTNode* A)
{
	if (A == NULL)
	{
		printf("NULL ");
		return;
	}
	Postorder(A->left);
	Postorder(A->right);
	printf("%c ", A->val);
}


int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	
	return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}

int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}

	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);

}

int BinaryTreeLevelKSize(BTNode* root, int k)
{

	if (root == NULL)
	{
		return 0;
	}
	if (k == 1)
	{
		return 1;
	}

	return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);

}



int BinaryTreeDepth(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}

	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}

	int left = BinaryTreeDepth(root->left);
	int right = BinaryTreeDepth(root->right);

	return 1 + (left > right ? left : right);

}

BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
	{
		return NULL;
	}
	if (root->val == x)
	{
		return root;
	}
	BTNode* left = BinaryTreeFind(root->left, x);
	if (left)
	{
		return left;
	}
	BTNode* right = BinaryTreeFind(root->right, x);
	if (right)
	{
		return right;
	}

	return NULL;
}


void BinaryTreeDestory(BTNode** root)
{
	if (*root == NULL)
	{
		return;
	}

	BinaryTreeDestory(&((*root)->left));
	BinaryTreeDestory(&((*root)->right));
	free(*root);
	*root = NULL;
}

void LevelOrder(BTNode* root)
{
	Queue qe;
	QueueInit(&qe);
	QueuePush(&qe, root);
	while (!QueueEmpty(&qe))
	{
		BTNode* tmp = QueueFront(&qe);
		printf("%c ", tmp->val);

		if (tmp->left)
		{
			QueuePush(&qe, tmp->left);

		}
		if (tmp->right)
		{
			QueuePush(&qe, tmp->right);

		}

		QueuePop(&qe);
	}
	QueueDestroy(&qe);
}

bool BinaryTreeComplete(BTNode* root)
{
	Queue qe;
	QueueInit(&qe);
	QueuePush(&qe, root);
	while (!QueueEmpty(&qe))
	{
		BTNode* tmp = QueueFront(&qe);
		QueuePop(&qe);

		if (tmp == NULL)
		{
			break;
		}
		QueuePush(&qe, tmp->left);
		QueuePush(&qe, tmp->right);
	}
	while (!QueueEmpty(&qe))
	{
		BTNode* tmp = QueueFront(&qe);
		QueuePop(&qe);
		if (tmp!=NULL)
		{
			QueueDestroy(&qe);
			return false;
		}
	}

	QueueDestroy(&qe);
	return true;
}
BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
{
	if (a[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}
	BTNode* root = BuyBTNode(a[*pi]);
	(*pi)++;

	root->left = BinaryTreeCreate(a, pi);
	root->right = BinaryTreeCreate(a, pi);
	return root;
}

